/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * Description
 *
 * User: jinchen
 * Date: Jun 2, 2003
 * Time: 11:28:35 AM
 */
package edu.psu.geovista.app.plot.ruler;

import edu.psu.geovista.app.plot.axis.ColorBar;
import edu.psu.geovista.app.plot.axis.Range;
import edu.psu.geovista.classification.Category;
import edu.psu.geovista.classification.CategoryList;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class ColorRuler extends DfRuler {
    public static final boolean DEBUG=false;
    //Color[] categoryColors;
    CategoryList ctgList;
    ColorBar cbar;
    boolean colorBarVisible;

    public ColorRuler(int orient, int length, Point op, Range range) {
        super(orient, length, op, range);
        if (ctgList!=null)
            this.createColorBar(ctgList);
    }



    public void paintComponent(Graphics g){
        if (isColorBarVisible()&&cbar!=null)//Must add condition && cbar!=null. It seems to be a minor bug
            cbar.paintComponent(g);//
        super.paintComponent(g);

    }
    /********************************************************************
     *                properties
     ********************************************************************/
    public void setCategoryList(CategoryList ctgList) {

        if (ctgList!=null&& ctgList.size() >0){
            this.ctgList = ctgList;
            this.createColorBar(ctgList);
        }
        else{
           ctgList=null;
           this.cbar=null;
        }

    }
    private void createColorBar(CategoryList ctgList){
        //Dimension
        int w=10;
        int h=super.getLength() ;
        Dimension size=new Dimension(w,h);
        //Position
        ArrayList paintCtgList=this.generatePaintableCategory(ctgList);
        this.cbar=new ColorBar(super.getLocation(),size,paintCtgList,true);
    }
    public boolean isColorBarVisible() {
        if (ctgList==null)
            colorBarVisible=false;
        else
            colorBarVisible=true;
        return colorBarVisible;
    }

    private   ArrayList generatePaintableCategory(CategoryList ctgList){

        ArrayList list=new ArrayList();
        Enumeration e=ctgList.getCtgList().elements() ;
        while(e.hasMoreElements() ){
            Category ctg=(Category) e.nextElement() ;
            if(!Float.isNaN( ctg.getMin() )){
                // 
                //It may happen since Visual Classifier is designed to allow use set as many category as they want
                int MinPaintValue=this.getGraphicValueOf(ctg.getMin() );//
                Category dummy=new Category();//dummy is a dummy category only for painting purpose

                dummy.setColor(ctg.getColor() );
                dummy.setMin( MinPaintValue) ;
                //dp{
                if (DEBUG){
                    System.out.println("category data range:"+dummy.getMin() +" - "+dummy.getMax() );
                }//dp}
                list.add(dummy);
            }
        }
        Collections.sort(list);
        return list;

    }
}
